//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.goldhuman.IO.Protocol;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class Parser {
    private static SAXParser sap;
    private static URL config = Parser.class.getResource("/config.xml");
    public static final String default_package = "protocol";
    public static final String rpc_default_package = "rpc";
    public static final String rpcdata_default_package = "rpcdata";
    private static boolean bl_gen_with_seperate_dir = false;

    static {
        try {
            sap = SAXParserFactory.newInstance().newSAXParser();
        } catch (Exception var1) {
            var1.printStackTrace();
        }

    }

    public Parser() {
    }

    public void setConfig(URL _config) {
        config = _config;
    }

    private static PrintStream find_file(String name) {
        String cname = "protocol/" + name;
        File file = new File(cname + ".java");
        if (file.exists()) {
            if (!(new File(cname + ".class")).exists()) {
                System.err.println("Compile " + cname + ".java first...");
            }

            return null;
        } else {
            System.out.println("Generate " + file.getName());

            try {
                return new PrintStream(new FileOutputStream(file));
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        }
    }

    private static PrintStream find_file_rpcdata(String name) {
        String dirname = bl_gen_with_seperate_dir ? "rpcdata" : "protocol";
        String cname = dirname + "/" + name;
        File file = new File(cname + ".java");
        if (file.exists()) {
            if (!(new File(cname + ".class")).exists()) {
                System.err.println("Compile " + cname + ".java first...");
            }

            return null;
        } else {
            System.out.println("Generate " + file.getName());

            try {
                return new PrintStream(new FileOutputStream(file));
            } catch (Exception var5) {
                var5.printStackTrace();
                return null;
            }
        }
    }

    private static PrintStream find_file_rpc(String name) {
        String dirname = bl_gen_with_seperate_dir ? "rpc" : "protocol";
        String cname = dirname + "/" + name;
        File file = new File(cname + ".java");
        if (file.exists()) {
            if (!(new File(cname + ".class")).exists()) {
                System.err.println("Compile " + cname + ".java first...");
            }

            return null;
        } else {
            System.out.println("Generate " + file.getName());

            try {
                return new PrintStream(new FileOutputStream(file));
            } catch (Exception var5) {
                var5.printStackTrace();
                return null;
            }
        }
    }

    private static void generate_import(PrintStream ps) {
        ps.println("package protocol;\n");
        ps.println("import com.goldhuman.Common.*;");
        ps.println("import com.goldhuman.Common.Marshal.*;");
        ps.println("import com.goldhuman.Common.Security.*;");
        ps.println("import com.goldhuman.IO.Protocol.*;");
        ps.println("");
    }

    private static void rpc_generate_import(PrintStream printstream) {
        if (bl_gen_with_seperate_dir) {
            printstream.println("package rpc;\n");
        } else {
            printstream.println("package protocol;\n");
        }

        printstream.println("import com.goldhuman.Common.*;");
        printstream.println("import com.goldhuman.Common.Marshal.*;");
        printstream.println("import com.goldhuman.Common.Security.*;");
        printstream.println("import com.goldhuman.IO.Protocol.*;");
        printstream.println("");
    }

    private static void rpcdata_generate_import(PrintStream printstream) {
        if (bl_gen_with_seperate_dir) {
            printstream.println("package rpcdata;\n");
        } else {
            printstream.println("package protocol;\n");
        }

        printstream.println("import com.goldhuman.Common.*;");
        printstream.println("import com.goldhuman.Common.Marshal.*;");
        printstream.println("import com.goldhuman.Common.Security.*;");
        printstream.println("import com.goldhuman.IO.Protocol.*;");
        printstream.println("");
    }

    private static void generate_datamember(PrintStream ps, Vector<Variable> variables) {
        int size = variables.size();

        for(int i = 0; i < size; ++i) {
            Variable v = (Variable)variables.get(i);
            if (v.type.endsWith("Vector")) {
                ps.println("\tpublic DataVector\t" + v.name + ";");
            } else {
                ps.println("\tpublic " + v.type + "\t" + v.name + ";");
            }
        }

        ps.println("");
    }

    private static void generate_marshalable(PrintStream ps, Vector<Variable> variables) {
        ps.println("\tpublic OctetsStream marshal(OctetsStream os)");
        ps.println("\t{");
        int size = variables.size();

        int i;
        Variable v;
        for(i = 0; i < size; ++i) {
            v = (Variable)variables.get(i);
            ps.println("\t\tos.marshal(" + v.name + ");");
        }

        ps.println("\t\treturn os;\n\t}");
        ps.println("");
        ps.println("\tpublic OctetsStream unmarshal(OctetsStream os) throws MarshalException");
        ps.println("\t{");

        for(i = 0; i < size; ++i) {
            v = (Variable)variables.get(i);
            String type = v.type;
            if (type.compareTo("int") != 0 && type.compareTo("short") != 0 && type.compareTo("long") != 0 && type.compareTo("byte") != 0 && type.compareTo("float") != 0 && type.compareTo("double") != 0) {
                ps.println("\t\tos.unmarshal(" + v.name + ");");
            } else {
                ps.println("\t\t" + v.name + " = os.unmarshal_" + type + "();");
            }
        }

        ps.println("\t\treturn os;\n\t}");
        ps.println("");
    }

    private static void generate_defaultconstructor(PrintStream ps, String name, Vector<Variable> variables) {
        ps.println("\tpublic " + name + "()");
        ps.println("\t{");
        int size = variables.size();

        for(int i = 0; i < size; ++i) {
            Variable v = (Variable)variables.get(i);
            String type = v.type;
            String key = v.name;
            if (type.compareTo("int") != 0 && type.compareTo("short") != 0 && type.compareTo("long") != 0 && type.compareTo("byte") != 0 && type.compareTo("float") != 0 && type.compareTo("double") != 0) {
                if (v.type.endsWith("Vector")) {
                    ps.println("\t\t" + key + " = new DataVector(" + "new " + type.replaceAll("Vector", "") + "());");
                } else {
                    ps.println("\t\t" + key + " = new " + type + "();");
                }
            }
        }

        ps.println("\t}");
        ps.println("");
    }

    private static void generate_cloneable(PrintStream ps, String name, Vector<Variable> variables) {
        ps.println("\tpublic Object clone()");
        ps.println("\t{");
        ps.println("\t\ttry");
        ps.println("\t\t{");
        ps.println("\t\t\t" + name + " o = (" + name + ")super.clone();");
        int size = variables.size();

        for(int i = 0; i < size; ++i) {
            Variable v = (Variable)variables.get(i);
            String type = v.type;
            String key = v.name;
            if (type.compareTo("int") != 0 && type.compareTo("short") != 0 && type.compareTo("long") != 0 && type.compareTo("byte") != 0 && type.compareTo("float") != 0 && type.compareTo("double") != 0) {
                if (v.type.endsWith("Vector")) {
                    ps.println("\t\t\to." + key + " = (DataVector)" + key + ".clone();");
                } else {
                    ps.println("\t\t\to." + key + " = (" + type + ")" + key + ".clone();");
                }
            }
        }

        ps.println("\t\t\treturn o;");
        ps.println("\t\t}");
        ps.println("\t\tcatch (Exception e) { }");
        ps.println("\t\treturn null;\n\t}");
        ps.println("");
    }

    private static void protocol_generate(String name, Vector<Variable> variables) {
        PrintStream ps = find_file(name);
        if (ps != null) {
            generate_import(ps);
            ps.println("public final class " + name + " extends Protocol");
            ps.println("{");
            generate_datamember(ps, variables);
            generate_defaultconstructor(ps, name, variables);
            generate_marshalable(ps, variables);
            generate_cloneable(ps, name, variables);
            ps.println("\tpublic void Process(Manager manager, Session session) throws ProtocolException");
            ps.println("\t{");
            ps.println("\t}");
            ps.println("");
            ps.println("}");
            ps.close();
        }
    }

    private static void rpcdata_generate(String name, Vector<Variable> variables) {
        PrintStream ps = find_file_rpcdata(name);
        if (ps != null) {
            rpcdata_generate_import(ps);
            ps.println("public final class " + name + " extends Rpc.Data");
            ps.println("{");
            generate_datamember(ps, variables);
            generate_defaultconstructor(ps, name, variables);
            generate_marshalable(ps, variables);
            generate_cloneable(ps, name, variables);
            ps.println("}");
            ps.close();
        }
    }

    private static void rpc_generate(String name, String argument, String result) {
        PrintStream ps = find_file_rpc(name);
        if (ps != null) {
            rpc_generate_import(ps);
            ps.println("public final class " + name + " extends Rpc");
            ps.println("{");
            ps.println("\tpublic void Server(Data argument, Data result) throws ProtocolException");
            ps.println("\t{");
            ps.println("\t\t" + argument + " arg = (" + argument + ")argument;");
            ps.println("\t\t" + result + " res = (" + result + ")result;");
            ps.println("\t}");
            ps.println("");
            ps.println("\tpublic void Client(Data argument, Data result) throws ProtocolException");
            ps.println("\t{");
            ps.println("\t\t" + argument + " arg = (" + argument + ")argument;");
            ps.println("\t\t" + result + " res = (" + result + ")result;");
            ps.println("\t}");
            ps.println("");
            ps.println("\tpublic void OnTimeout()");
            ps.println("\t{");
            ps.println("\t}");
            ps.println("");
            ps.println("}");
            ps.close();
        }
    }

    protected static void ParseProtocol(final Map map) throws Exception {
        sap.parse(config.openStream(), new DefaultHandler() {
            private boolean parsing = false;
            private String name;
            private String type;
            private String class_name;
            private String maxsize;
            private String priority;
            private Vector<Variable> variables;

            public void startElement(String uri, String localName, String qName, Attributes attributes) {
                try {
                    if (qName.compareTo("protocol") == 0) {
                        this.parsing = true;
                        this.variables = new Vector();
                        this.name = attributes.getValue("name");
                        this.type = attributes.getValue("type");
                        this.class_name = attributes.getValue("class");
                        this.maxsize = attributes.getValue("maxsize");
                        this.priority = attributes.getValue("priority");
                        if (this.priority == null) {
                            this.priority = attributes.getValue("prior");
                        }
                    } else if (this.parsing && qName.compareTo("variable") == 0) {
                        String vtype = attributes.getValue("type");
                        vtype = vtype.replaceAll("unsigned", "").trim();
                        vtype = vtype.replaceAll("char", "byte").trim();
                        this.variables.add(new Variable(attributes.getValue("name"), vtype));
                    }
                } catch (Exception var6) {
                    var6.printStackTrace();
                }

            }

            public void endElement(String uri, String localName, String qName) {
                if (this.parsing && qName.compareTo("protocol") == 0) {
                    this.parsing = false;

                    try {
                        Protocol instance = (Protocol)Class.forName("protocol." + (this.class_name == null ? this.name : this.class_name)).newInstance();
                        instance.type = Integer.parseInt(this.type);
                        instance.size_policy = this.maxsize == null ? 0 : Integer.parseInt(this.maxsize);
                        instance.prior_policy = this.priority == null ? 0 : Integer.parseInt(this.priority);
                        if (map.put(this.name.toUpperCase(), instance) != null) {
                            System.err.println("Duplicate protocol name " + this.name);
                        }

                        if (map.put(this.type, instance) != null) {
                            System.err.println("Duplicate protocol type " + this.type);
                        }
                    } catch (Exception var5) {
                        Parser.protocol_generate(this.name, this.variables);
                    }

                    this.name = null;
                    this.type = null;
                    this.class_name = null;
                    this.maxsize = null;
                    this.priority = null;
                    this.variables = null;
                }

            }
        });
    }

    protected static void ParseState(final Map<String, State> map) throws Exception {
        final Map<String, String> typemap = new HashMap();
        sap.parse(config.openStream(), new DefaultHandler() {
            public void startElement(String uri, String localName, String qName, Attributes attributes) {
                if (qName.compareTo("protocol") == 0 || qName.compareTo("rpc") == 0) {
                    try {
                        typemap.put(attributes.getValue("name").trim(), attributes.getValue("type").trim());
                    } catch (Exception var6) {
                        var6.printStackTrace();
                    }

                }
            }
        });
        sap.parse(config.openStream(), new DefaultHandler() {
            private String state_name = null;
            private State state = null;

            public void startElement(String uri, String localName, String qName, Attributes attributes) {
                if (qName.compareTo("state") == 0) {
                    try {
                        this.state_name = attributes.getValue("name").trim().toUpperCase();

                        try {
                            this.state = new State(1000L * Long.parseLong(attributes.getValue("timeout").trim()));
                        } catch (Exception var6) {
                            this.state = new State(0L);
                        }
                    } catch (Exception var7) {
                    }
                }

                if (this.state_name != null && qName.compareTo("proto") == 0) {
                    this.state.AddProtocolType((String)typemap.get(attributes.getValue("name")));
                }

            }

            public void endElement(String uri, String localName, String qName) {
                if (qName.compareTo("state") == 0) {
                    if (this.state_name != null) {
                        map.put(this.state_name, this.state);
                    }

                    this.state_name = null;
                }

            }
        });
    }

    private static final Map ParseRpcData() throws Exception {
        final Map typemap = new HashMap();
        sap.parse(config.openStream(), new DefaultHandler() {
            private boolean parsing = false;
            private String rpcdataName = null;
            private Vector<Variable> variables;

            public void startElement(String uri, String localName, String qName, Attributes attributes) {
                try {
                    if (qName.compareTo("rpcdata") == 0) {
                        this.parsing = true;
                        this.variables = new Vector();
                        this.rpcdataName = attributes.getValue("name");
                    } else if (this.parsing && qName.compareTo("variable") == 0) {
                        String vtype = attributes.getValue("type");
                        vtype = vtype.replaceAll("unsigned", "").trim();
                        vtype = vtype.replaceAll("char", "byte").trim();
                        this.variables.add(new Variable(attributes.getValue("name"), vtype));
                    }
                } catch (Exception var6) {
                    var6.printStackTrace();
                }

            }

            public void endElement(String uri, String localName, String qName) {
                if (this.parsing && qName.compareTo("rpcdata") == 0) {
                    this.parsing = false;

                    try {
                        if (Parser.bl_gen_with_seperate_dir) {
                            typemap.put(this.rpcdataName, Class.forName("rpcdata." + this.rpcdataName).newInstance());
                        } else {
                            typemap.put(this.rpcdataName, Class.forName("protocol." + this.rpcdataName).newInstance());
                        }
                    } catch (Exception var5) {
                        Parser.rpcdata_generate(this.rpcdataName, this.variables);
                    }

                    this.rpcdataName = null;
                    this.variables = null;
                }

            }
        });
        return typemap;
    }

    protected static void ParseRpc(final Map map) throws Exception {
        final Map typemap = ParseRpcData();
        sap.parse(config.openStream(), new DefaultHandler() {
            public void startElement(String uri, String localName, String qName, Attributes attributes) {
                if (qName.compareTo("rpc") == 0) {
                    try {
                        String name = attributes.getValue("name");
                        String type = attributes.getValue("type");
                        String argument = attributes.getValue("argument");
                        String result = attributes.getValue("result");
                        String maxsize = attributes.getValue("maxsize");
                        String timeout = attributes.getValue("timeout");
                        String priority = attributes.getValue("priority");
                        if (priority == null) {
                            priority = attributes.getValue("prior");
                        }

                        Rpc rpc = null;

                        try {
                            if (Parser.bl_gen_with_seperate_dir) {
                                rpc = (Rpc)Class.forName("rpc." + name).newInstance();
                            } else {
                                rpc = (Rpc)Class.forName("protocol." + name).newInstance();
                            }
                        } catch (Exception var14) {
                            Parser.rpc_generate(name, argument, result);
                            return;
                        }

                        rpc.argument = (Rpc.Data)typemap.get(argument);
                        rpc.result = (Rpc.Data)typemap.get(result);
                        rpc.type = Integer.parseInt(type);
                        rpc.prior_policy = priority == null ? 0 : Integer.parseInt(priority);
                        rpc.time_policy = timeout == null ? 25000L : 1000L * Long.parseLong(timeout);
                        rpc.size_policy = maxsize == null ? 0 : Integer.parseInt(maxsize);
                        if (map.put(name.toUpperCase(), rpc) != null) {
                            System.err.println("Duplicate protocol name " + name);
                        }

                        if (map.put(type, rpc) != null) {
                            System.err.println("Duplicate protocol type " + type);
                        }
                    } catch (Exception var15) {
                        var15.printStackTrace();
                    }
                }

            }
        });
    }

    protected static void ParseFlag(Map map) throws Exception {
        sap.parse(config.openStream(), new DefaultHandler() {
            public void startElement(String uri, String localName, String qName, Attributes attributes) {
                if (qName.compareTo("dirflag") == 0) {
                    try {
                        String name = attributes.getValue("bl_gen_with_seperate_dir");
                        if (name != null && name.equalsIgnoreCase("true")) {
                            Parser.bl_gen_with_seperate_dir = true;
                        }
                    } catch (Exception var6) {
                        var6.printStackTrace();
                    }
                }

            }
        });
    }

    private static class Variable {
        public String name;
        public String type;

        public Variable(String name, String type) {
            this.name = name;
            this.type = type;
        }
    }
}
